package leetcode.median;

public class $6_ZConvert {
    public String convert(String s, int numRows) {
        int len = s.length();
        if (numRows <= 1 || len < numRows) {
            return s;
        }
        int row = numRows;

        int t = 2 * row - 2;
        char[][] matrix = new char[row][len];
        for (int i = 0, x = 0, y = 0; i < len; i++) {
            matrix[x][y] = s.charAt(i);
            if (i % t < row - 1) {
                x++;
            } else {
                y++;
                x--;
            }
        }
        StringBuilder sb = new StringBuilder();
        for (char[] r:matrix) {
            for (char c:r) {
                if (c != 0) {
                    sb.append(c);
                }
            }
        }
        return sb.toString();

    }
}
